// 导入依赖的模块
yepjs.require("jquery");
yepjs.require("YBA.Common");
yepjs.require("YBA.Plugin.plugins");
yepjs.require("YBA.Data.Statistics");


// 安全审计 - 任务执行情况
yepjs.define("taskExecute", {

    NS: "YBA.Logic.Statistics",

    field: function() {

        var Biz = {
            // 初始化事件
            init: function() {
                Biz.fn.getTaskExecute(Biz.vars.data);
                for (var i in Biz.event) {
                    Biz.event[i]();
                }
            },
            //  公共变量
            vars: {
                // 模板列表
                tpl: (function(){
                    var tpl = {};
                    $("script[type='text/ysmart']").each(function(){
                        var t = $(this);
                        tpl[t.attr("data-name")] = t.html();
                    });
                    return tpl;
                })(),
                //暂定空参
                data:{}
            },
            // 页面元素
            el: {
                jtodayList:$("#j-todayList"),
                jhistoryList:$("#j-historyList")
            },
            // 功能函数集合
            fn: {
                //获取任务执行情况
                getTaskExecute:function(data){
                    YBA.Data.Statistics.taskExecute(data,function(json){
                        if(json.code == 200) {
                            Biz.el.jtodayList.html(YBA.Common.ysmart(Biz.vars.tpl.todayTpl, json.taskTodayExecuteCase));
                            Biz.el.jhistoryList.html(YBA.Common.ysmart(Biz.vars.tpl.historyTpl, json.taskHistoryExecuteCase));
                        }else{
                            YBA.Plugin.msgtip({type: "i", msg: json.desc})
                        }
                    })
                },
                //执行数据源信息列表弹窗
                executeForm:function(json){
                    var popform = new YBA.Plugin.popform({
                        title:"查看数据源信息",
                        className:"ui-dialog-execute",
                        content:YBA.Common.ysmart(Biz.vars.tpl.executeTpl,json || {}),
                        width:800,
                        destroy:true,
                        onHide: function () {
                            // 隐藏弹窗时要执行的回调函数
                        },
                        onShow:function(){
                            // 显示弹窗时要执行的回调函数
                            $(".ui-dialog-content").css({
                                maxHeight: '500px',
                                overflow: 'auto'
                            });
                        }
                    }).show();
                }
            },
            // 初始化时执行的所有事件
            event: {
                //获取当前的终端今日执行数据源信息列表
                terminalDatasourceExecuteToday:function(){
                    Biz.el.jtodayList.delegate(".j-todayExecute","click", function() {
                        var t = $(this), tr = t.closest("tr"),id = tr.attr("data-id");
                        var data = {
                            "terminalId":id
                        }
                        YBA.Data.Statistics.terminalDatasourceExecuteToday(data,function(json){
                            if(json.code == 200) {
                                Biz.fn.executeForm(json.executeDataSourceList);
                            }else{
                                YBA.Plugin.msgtip({type: "i", msg: json.desc});
                            }
                        })
                    });
                },
                //获取当前的终端历史执行数据源信息列表
                terminalDatasourceExecuteHistory:function(){
                    Biz.el.jhistoryList.delegate(".j-historyExecute","click", function() {
                        var t = $(this), tr = t.closest("tr"),id = tr.attr("data-id");
                        var data = {
                            "terminalId":id
                        };
                        YBA.Data.Statistics.terminalDatasourceExecuteHistory(data,function(json){
                            if(json.code == 200) {
                                Biz.fn.executeForm(json.executeDataSourceList);
                            }else{
                                YBA.Plugin.msgtip({type: "i", msg: json.desc});
                            }
                        })
                    });
                }
            }
        };

        Biz.init();

        return Biz;
    }
});
